package com.dios.mirage.component.view;

import java.util.Vector;

import net.rim.device.api.math.Fixed32;
import net.rim.device.api.system.EncodedImage;
import net.rim.device.api.ui.Manager;
import net.rim.device.api.ui.container.HorizontalFieldManager;
import net.rim.device.api.ui.container.VerticalFieldManager;

import com.dios.mirage.component.fields.ContentField;
import com.dios.mirage.component.fields.HoverField;

public abstract class View extends VerticalFieldManager {

	public static final String BLACK = "Black";
	public static final String WHITE = "White";
	public static final String SILVER = "Silver";
	public static final String GREEN = "Green";
	public static final String YELLOW = "Yellow";
	public static final String RED = "Red";
	public static final String GREY = "Grey";

	protected String contentPath;
	protected String basePath = "img/";

	protected VerticalFieldManager content;
	protected ContentField contentField;
	protected HoverField hoverField;

	protected Vector entries = new Vector();
	protected Vector slideImage = new Vector();
	protected Vector descText = new Vector();
	protected Vector titleText = new Vector();
	protected HorizontalFieldManager list;

	public View() {
		content = new VerticalFieldManager(Manager.HORIZONTAL_SCROLL);
		list = new HorizontalFieldManager(Manager.HORIZONTAL_SCROLL);
		contentField = new ContentField();
		content.add(contentField);
		createGUI();
	}

	public void setBasePath(String basePath) {
		this.basePath = basePath;
	}

	public EncodedImage getImage(String file) {
		String path = basePath.concat(file).concat(".jpg");
		return EncodedImage.getEncodedImageResource(path);
	}

	public EncodedImage getImage(String file, int width, int height) {
		String path = basePath.concat(file).concat(".jpg");
		EncodedImage image = EncodedImage.getEncodedImageResource(path);
		image = sizeImage(image, width, height);
		return image;
	}

	public static EncodedImage createImage(String path, int width, int height) {
		EncodedImage image = EncodedImage.getEncodedImageResource(path);
		if (height == 0)
			height = image.getHeight();
		image = sizeImage(image, width, height);
		return image;
	}

	public void addImage(String text, String desc, String hover, String slide,
			boolean firstImage) {
		HoverField hoverField = new HoverField(text, desc, slide, this);
		hoverField.setImage(getImage(hover));
		if (firstImage)
			setContent(slide, text, desc);
		list.add(hoverField);
	}

	public abstract void setContent(String slide, String text, String desc);

	private static EncodedImage sizeImage(EncodedImage image, int width,
			int height) {
		EncodedImage result = null;

		int currentWidthFixed32 = Fixed32.toFP(image.getWidth());
		int currentHeightFixed32 = Fixed32.toFP(image.getHeight());

		int requiredWidthFixed32 = Fixed32.toFP(width);
		int requiredHeightFixed32 = Fixed32.toFP(height);

		int scaleXFixed32 = Fixed32.div(currentWidthFixed32,
				requiredWidthFixed32);
		int scaleYFixed32 = Fixed32.div(currentHeightFixed32,
				requiredHeightFixed32);

		result = image.scaleImage32(scaleXFixed32, scaleYFixed32);
		return result;
	}

	public abstract void createGUI();
}
